package com.stylefeng.guns.modular.hhtb.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.shiro.ShiroUser;
import com.stylefeng.guns.modular.hhtb.model.UsersRechargeDTO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.hhtb.model.UsersRecharge;
import com.stylefeng.guns.modular.hhtb.service.IUsersRechargeService;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 用户充值控制器
 *
 * @author fengshuonan
 * @Date 2018-09-15 10:13:42
 */
@Controller
@RequestMapping("/usersRecharge")
public class UsersRechargeController extends BaseController {

    private String PREFIX = "/hhtb/usersRecharge/";

    @Autowired
    private IUsersRechargeService usersRechargeService;

    /**
     * 跳转到用户充值首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "usersRecharge.html";
    }

    /**
     * 跳转到添加用户充值
     */
    @RequestMapping("/usersRecharge_add")
    public String usersRechargeAdd() {
        return PREFIX + "usersRecharge_add.html";
    }

    /**
     * 跳转到修改用户充值
     */
    @RequestMapping("/usersRecharge_update/{usersRechargeId}")
    public String usersRechargeUpdate(@PathVariable Integer usersRechargeId, Model model) {
        UsersRecharge usersRecharge = usersRechargeService.selectById(usersRechargeId);
        String orderImgs = usersRecharge.getOrderImg();
        String[] split = orderImgs.split(",");
        for(int i = 0; i < split.length; i++){
            model.addAttribute("img"+i,split[i]);
        }
        model.addAttribute("item",usersRecharge);
        LogObjectHolder.me().set(usersRecharge);
        return PREFIX + "usersRecharge_edit.html";
    }

    /**
     * 获取用户充值列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(UsersRechargeDTO usersRechargeDTO) {
        Page<UsersRechargeDTO> page = new PageFactory<UsersRechargeDTO>().defaultPage();
        List<UsersRechargeDTO> list = usersRechargeService.seelctUsersRechargeAll(page,usersRechargeDTO);
        page.setRecords(list);
        return super.packForBT(page);
    }

    /**
     * 新增用户充值
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(UsersRecharge usersRecharge) {
        usersRechargeService.insert(usersRecharge);
        return SUCCESS_TIP;
    }

    /**
     * 删除用户充值
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer usersRechargeId) {
        usersRechargeService.deleteById(usersRechargeId);
        return SUCCESS_TIP;
    }

    /**
     * 修改用户充值
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(UsersRecharge usersRecharge) {
        ShiroUser shiroUser = (ShiroUser) super.getSession().getAttribute("shiroUser");
         Integer shiroUserId=null;
        if(shiroUser!=null){
            shiroUserId=  shiroUser.getId();
        }
        usersRecharge.setCheckStaffId(shiroUserId);
        Map<String, Object> resultMap = usersRechargeService.updateUsersRecharge(usersRecharge);
        if ("01".equals(resultMap.get("code").toString())){
            return SUCCESS_TIP;
        }
        return  null;
    }

    /**
     * 用户充值详情
     */
    @RequestMapping(value = "/detail/{usersRechargeId}")
    @ResponseBody
    public Object detail(@PathVariable("usersRechargeId") Integer usersRechargeId) {
        return usersRechargeService.selectById(usersRechargeId);
    }
}
